package com.yeyks.app.controller.base.consumer;

import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.utils.ArgumentsCheckUtil;
import com.yeyks.common.utils.UnicodeUtils;
import com.yeyks.consumer.dal.domain.Feedback;
import com.yeyks.consumer.dal.domain.UserInfo;
import com.yeyks.consumer.service.FeedbackService;
import com.yeyks.consumer.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author fuchangshuai
 * @date 2019/3/26 4:29 PM
 */
@Api(tags = "用户反馈接口", description = "反馈")
@Slf4j
@RestController("appFeedbackController")
@RequestMapping(value = "/app/feedback")
public class FeedbackController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private FeedbackService feedbackService;

    /**
     * 增加一条反馈
     *
     * @param token
     * @param content
     * @param title
     * @return
     */
    @ApiOperation(value = "增加一条反馈", notes = "增加一条反馈")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "content", value = "内容", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "title", value = "标题", required = true, dataType = "String")
    })
    @PostMapping(value = "/add")
    public AjaxResponseBody add(@RequestParam(name = "token") String token,
                                @RequestParam(name = "content") String content,
                                @RequestParam(name = "title") String title) {
        if (ArgumentsCheckUtil.notEmpty(title)) {
            title = title.replaceAll("\\n", "");
            title = title.replaceAll("\\s", "");
        }
        if (ArgumentsCheckUtil.notEmpty(content)) {
            content = content.replaceAll("\\n", "");
            content = content.replaceAll("\\s", "");
        }
        if (!ArgumentsCheckUtil.notEmpty(title)) {
            throw new ServiceException("标题输入不能为空");
        }
        if (!ArgumentsCheckUtil.notEmpty(content)) {
            throw new ServiceException("内容输入不能为空");
        }
        if (title.length() > 15) {
            throw new ServiceException("标题长度不能大于15");
        }
        if (content.length() > 180) {
            throw new ServiceException("内容长度不能大于180");
        }
        content = UnicodeUtils.unicodeToString(content);
        title = UnicodeUtils.unicodeToString(title);
        UserInfo userInfo = userInfoService.tokenUserInfo(token);
        Feedback feedback = new Feedback();
        feedback.setUserId(userInfo.getId());
        feedback.setUserName(userInfo.getUsername());
        feedback.setUserPhone(userInfo.getPhone());
        feedback.setContent(content);
        feedback.setTitle(title);
        feedback.setReport(false);
        feedback.setReaded(false);
        Date date = new Date();
        feedback.setGmtCreate(date);
        feedback.setGmtModified(date);
        if (!feedbackService.save(feedback)) {
            throw new ServiceException("更新反馈信息失败");
        }
        return AjaxResponseBody.success();
    }
}
